package com.bl.org.service.impl;

import com.bl.basic.exception.BusinessException;
import com.bl.basic.util.BaiduAuditUtils;
import com.bl.org.domain.Employee;
import com.bl.org.domain.Shop;
import com.bl.org.domain.ShopAuditLog;
import com.bl.org.dto.ShopDto;
import com.bl.org.mapper.EmployeeMapper;
import com.bl.org.mapper.ShopAuditLogMapper;
import com.bl.org.mapper.ShopMapper;
import com.bl.org.service.IShopService;
import com.bl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     *  店铺入驻业务 实现
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        // 1 校验空值
        if (StringUtils.isEmpty(shop.getName())
            || StringUtils.isEmpty(shop.getTel())
            || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getAdmin().getUsername())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            // 提示店铺商家，参数不能为空
            throw new BusinessException("参数不能为空");
        }

        Employee employee = shop.getAdmin();
        // 2. 校验两次密码一致
        if (!employee.getPassword().equals(employee.getComfirmPassword())){

            throw  new BusinessException("两次密码不一致");
        }
        // 检查店铺名称是否合规
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoolean){
            throw  new BusinessException("！！！店铺名称不合规");
        }
        // 3. 店铺是否入驻过
        Shop dbShop = shopMapper.getShopByMessage(shop);
        if(dbShop!=null){
            throw new BusinessException("该店铺已经被注册过！");
        }

        // 5. 保存店铺管理员信息t_employee
        employeeMapper.save(employee);
        shop.setAdminId(employee.getId());

        // 6.  保存店铺信息t_shop
        shopMapper.save(shop);
        employee.setShopId(shop.getId());
        // 7. 将店铺的id更新到t_employee中
        employeeMapper.update(employee);
    }

    /**
     * 店铺通过审核发送邮件
     * @param log
     * @throws MessagingException
     */
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        // 修改状态
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        // 添加日志
        log.setState(2);
        log.setAuditId(22L);
        shopAuditLogMapper.save(log);
        // 发送激活邮件
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("18160016785@163.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
                "<img src='http://123.207.27.208/"+shop.getLogo()+"' >" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //收件人
        // 获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        String email = employee.getEmail();
        // helper.setTo(email);
        System.out.println(email);
        helper.setTo("15528351994@163.com");
        javaMailSender.send(mimeMessage);
    }


    /**
     * 店铺驳回审核发送邮件
     * @param log
     * @throws MessagingException
     */
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        // 修改状态
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        // 保存日志
        log.setState(4);
        log.setAuditId(22L);
        shopAuditLogMapper.save(log);
        // 发送通知邮件
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("18160016785@163.com");
        helper.setSubject("店铺激活失败邮件");
        helper.setText("<h1>很抱歉！！！你的店铺注册失败!!!</h1>",true);
        //收件人
        // 获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        String email = employee.getEmail();
        // helper.setTo(email);
        System.out.println(email);
        helper.setTo("15528351994@163.com");
        javaMailSender.send(mimeMessage);

        //4.删除入驻信息
        //删除管理员信息
        employeeMapper.remove(shop.getAdminId());
        //删除店铺信息
        shopMapper.remove(shop.getId());
    }

    /**
     *  激活店铺 到登陆页面 并修改状态
     * @param id
     */
    @Transactional
    @Override
    public void active(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(3);
        shopMapper.update(shop);
    }

    /**
     * getEcharts数据
     * @return
     */
    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }


}



